package com.qf.leadnewswemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.leadnewswemedia.mapper.WmMaterialMapper;
import com.qf.leadnewswemedia.service.WmMaterialService;
import com.qf.qfleadnewscommons.minio.MinioOperator;
import com.qf.qfleadnewscommons.mvc.ThreadLocalUtils;
import com.qf.qfleadnewsmodel.commons.dtos.PageResponseResult;
import com.qf.qfleadnewsmodel.commons.dtos.ResponseResult;
import com.qf.qfleadnewsmodel.enums.AppHttpCodeEnum;
import com.qf.qfleadnewsmodel.wemedia.dtos.MaterialPageDto;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmMaterial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinioOperator minioOperator;

    @Override
    public ResponseResult pageQuery(MaterialPageDto materialPageDto) {

        Integer isCollection = materialPageDto.getIsCollection();

        IPage<WmMaterial> page = new Page<>(materialPageDto.getPage(),materialPageDto.getSize());

        Wrapper<WmMaterial> qw = Wrappers.lambdaQuery(WmMaterial.class)
                        //只能查询当前登录用户的素材
                        .eq(WmMaterial::getUserId, ThreadLocalUtils.getUid())
                        //如果是只查询收藏的素材，就需要带上这个条件
                        .eq(isCollection!=0,WmMaterial::getIsCollection,isCollection)
                        //根据素材上传时间进行降序排列
                        .orderByDesc(WmMaterial::getCreatedTime);

        page(page,qw);

        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setCode(200);
        pageResponseResult.setData(page.getRecords());

        pageResponseResult.setTotal(page.getTotal());
        pageResponseResult.setSize(page.getSize());
        pageResponseResult.setCurrentPage(page.getCurrent());

        return pageResponseResult;
    }

    @Override
    public ResponseResult upload(MultipartFile multipartFile, HttpServletRequest request) throws IOException {

        if (multipartFile == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //将素材保存到分布式云存储中
        InputStream is = multipartFile.getInputStream();
        String originalFilename = multipartFile.getOriginalFilename();
        String url = minioOperator.upload(is,originalFilename,request);

        if (url == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIASL_UPLOAD_FAIL);
        }

        //将上传的素材添加到素材表中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(ThreadLocalUtils.getUid().intValue());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());

        save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }
}
